//
//  MyWeatherView.m
//  QuickCure
//
//  Created by 魏根 on 16/9/14.
//  Copyright © 2016年 haoge. All rights reserved.
//

#import "MyWeatherView.h"
#import "ProvinceController.h"
#import "NSString+Extension.h"
#import "NetWorkTool.h"


#define screenW [UIScreen mainScreen].bounds.size.width


@interface MyWeatherView ()

@property(nonatomic,strong) ProvinceController *proController;

@property(nonatomic,strong)UIImageView *myWeatherView;

@property(nonatomic,strong)UIImageView *weatherIcon;

@property(nonatomic,strong)UILabel *weatherLabel_1;

@property(nonatomic,strong)UIButton *cityBtn;

@property(nonatomic,strong)UILabel *temperatureLabel;

@property(nonatomic,strong)UILabel *dateLabel;


@end

@implementation MyWeatherView

- (instancetype)init{
    
    if (self = [super init]) {
     
        [self addMyWeather];
        
        [self myFontSetter];
    }
    return self;
}


- (void)addMyWeather{

     //天气view
    _myWeatherView = [[UIImageView alloc]init];

    [self addSubview:_myWeatherView];

    _myWeatherView.userInteractionEnabled = YES;

    [_myWeatherView mas_makeConstraints:^(MASConstraintMaker *make) {

        make.edges.equalTo(self);

    }];

    _myWeatherView.backgroundColor = [UIColor colorWithRed:0.6706 green:0.9098 blue:0.8235 alpha:1.0];

    //天气图片
    _weatherIcon = [[UIImageView alloc]init];

    [_myWeatherView addSubview:_weatherIcon];

    [_weatherIcon mas_makeConstraints:^(MASConstraintMaker *make) {

        make.centerY.equalTo(_myWeatherView);

        make.leading.equalTo(_myWeatherView).offset(20);

        make.width.height.equalTo(@40);

    }];

    _weatherIcon.image  = [UIImage imageNamed:@"zhenyu"];
    //天气状况
    _weatherLabel_1 = [[UILabel alloc]init];

    [_myWeatherView addSubview:_weatherLabel_1];

    CGSize myLabelSize = [@"天气晴" sizeWithFont:[UIFont systemFontOfSize:15] maxSize:CGSizeMake(60, 60)];
    _weatherLabel_1.numberOfLines = 2;

    [_weatherLabel_1 mas_makeConstraints:^(MASConstraintMaker *make) {

        make.centerY.equalTo(_myWeatherView);

        make.leading.equalTo(_weatherIcon.trailing).offset(10);

        make.width.equalTo(myLabelSize.width);

    }];

    _weatherLabel_1.text = @"上海";

    

    //中心的竖线
    UIView *line = [[UIView alloc]init];

    [self addSubview:line];

    [line makeConstraints:^(MASConstraintMaker *make) {

        make.width.equalTo(1);

        make.centerX.equalTo(self);

        make.top.equalTo(self).offset(10);

        make.bottom.equalTo(self).offset(-10);

    }];

    line.backgroundColor = [UIColor colorWithRed:0.6 green:0.6 blue:0.6 alpha:1.0];

    //地点按钮 增加下划线
    _cityBtn = [[UIButton alloc]init];
    
    [_myWeatherView addSubview:_cityBtn];
    
    [_cityBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.equalTo(_myWeatherView);
        
        make.trailing.equalTo(line.leading).offset(-8);
        
        make.width.height.equalTo(@50);
        
    }];
    
    NSMutableAttributedString *titleString = [[NSMutableAttributedString alloc]initWithString:@"上海" attributes:@{NSUnderlineStyleAttributeName:[NSNumber numberWithInteger:NSUnderlineStyleSingle]}];
    
    [_cityBtn setAttributedTitle:titleString forState:UIControlStateNormal];
    
    [_cityBtn addTarget:self action:@selector(provinceClick:) forControlEvents:UIControlEventTouchUpInside];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        [self loadWeatherWithCitys:_weatherLabel_1.text];
        
    });
    
    
    //温度标签
    _temperatureLabel = [[UILabel alloc]init];

    [_myWeatherView addSubview:_temperatureLabel];

    [_temperatureLabel makeConstraints:^(MASConstraintMaker *make) {

        make.top.equalTo(_myWeatherView).offset(15);

        make.centerX.equalTo(line).offset(screenW / 4);

    }];
    
    _temperatureLabel.text = @"温度: 26 ~ 23℃";

    //日期标签
    _dateLabel = [[UILabel alloc]init];

    [_myWeatherView addSubview:_dateLabel];

    [_dateLabel makeConstraints:^(MASConstraintMaker *make) {

        make.bottom.equalTo(_myWeatherView).offset(-15);

        make.centerX.equalTo(line).offset(screenW / 4);

    }];
    
    //日期占位 ---不需要从网络上获取了
    NSDate *todayDate = [NSDate date];
    
    NSCalendar *myCalendar = [NSCalendar currentCalendar];
    
    NSDateComponents *myComponents = [myCalendar components:(NSCalendarUnitWeekday | NSCalendarUnitMonth | NSCalendarUnitDay) fromDate:todayDate];
    
    
    NSDateFormatter *myFormatter = [[NSDateFormatter alloc]init];
    
    myFormatter.dateFormat = @"MM月dd日";
    
    todayDate = [myCalendar dateFromComponents:myComponents];
    
    NSString *dateString = [myFormatter stringFromDate:todayDate];
    
    NSArray *myWeekDay = @[@"日",@"一",@"二",@"三",@"四",@"五",@"六"];
    //@"日期: 周五 09月16日"
    _dateLabel.text = [NSString stringWithFormat:@"日期: 周%@ %@",myWeekDay[myComponents.weekday],dateString];

}

//设置字体属性
- (void)myFontSetter{

    UILabel *myLabel = [UILabel appearance];

    myLabel.font = [UIFont systemFontOfSize:15];

    myLabel.textColor = [UIColor colorWithRed:0.2667 green:0.6667 blue:0.6667 alpha:1.0];

   _cityBtn.titleLabel.font = [UIFont systemFontOfSize:15];

    NSMutableAttributedString *titleString = [[NSMutableAttributedString alloc]initWithString:_cityBtn.titleLabel.text attributes:@{NSUnderlineStyleAttributeName:[NSNumber numberWithInteger:NSUnderlineStyleSingle]}];

    [titleString addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:0.2667 green:0.6667 blue:0.6667 alpha:1.0] range:NSMakeRange(0, [titleString length])];

    [_cityBtn setAttributedTitle:titleString forState:UIControlStateNormal];

}

//切换省市数据按钮
- (void)provinceClick:(UIButton *)btn{

    _proController = [[ProvinceController alloc]init];
    
    __weak MyWeatherView *weakSelf = self;

    _proController.provinceBlock = ^(NSString *myCityName){

        [weakSelf loadWeatherWithCitys:myCityName];

    };
    
    if (self.weatherBlock) {
        
        self.weatherBlock(_proController);
    }


}

- (void)loadWeatherWithCitys:(NSString *)city {

    NSString *url = @"http://api.map.baidu.com/telematics/v3/weather";

    NSDictionary *paramDic = @{@"location":city,
                               @"output":@"json",
                               @"ak":@"17IvzuqmKrK1cGwNL6VQebF9"
                               };

    [[NetWorkTool shareManager] GET_RequestWithUrlString:url parmas:paramDic successBloock:^(id responseObject) {

        NSDictionary *dict = responseObject;

        NSArray *results = dict[@"results"];

        NSDictionary *weather_data = results[0];

        NSArray *weather_data_2 = weather_data[@"weather_data"];

        //今天的天气数据
        NSDictionary *todayWeatherData = weather_data_2[0];

        //赋值
        [_weatherIcon sd_setImageWithURL:[NSURL URLWithString:todayWeatherData[@"dayPictureUrl"]]];

        _weatherLabel_1.text = todayWeatherData[@"weather"];

        NSAttributedString *titleString = [[NSAttributedString alloc]initWithString:city attributes:@{NSUnderlineStyleAttributeName:[NSNumber numberWithInteger:NSUnderlineStyleSingle]}];

        [_cityBtn setAttributedTitle:titleString forState:UIControlStateNormal];

        _temperatureLabel.text = [NSString stringWithFormat:@"温度: %@",todayWeatherData[@"temperature"]];

    } failureBlock:^(NSError *error) {

        NSLog(@"天气错误= %@",error);
    }];

}

- (void)dealloc{
    
    NSLog(@"MyWeatherView dealloc...");
}


@end
